<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <!-- 退房列表 -->
        <el-table v-loading="loading" :data="tableData" ref="tableRef" border style="width: 90%" class="mb20">
          <el-table-column prop="capsuleRoomName" label="太空舱房间" align="center">
          </el-table-column>
          <el-table-column prop="capsuleRoomId" label="退房ID" align="center">
          </el-table-column>
          <el-table-column prop="webUserName" label="退房用户" align="center">
          </el-table-column>
          <el-table-column prop="webUserId" label="用户ID" align="center">
          </el-table-column>
          <el-table-column prop="checkOutDesc" label="退房原因" align="center">
          </el-table-column>
          <el-table-column prop="checkOutTime" label="申请时间" align="center">
          </el-table-column>
          <el-table-column prop="approveStatus" label="审批状态" align="center" width="120">
            <template slot-scope="scope">
              <div class="roomStatus" @click="scope.row.approveStatus == 0 ? fromCheckOut(scope.row.id) :  ''"
                :style="{background:scope.row.approveStatus == 0 ? '#f56c6c' : (scope.row.approveStatus == 1 ? '#5ac725' : (scope.row.approveStatus == '2' ? '#f9ae3d' : '#909399'))}">
                {{scope.row.approveStatus == 0 ? '审批中' : (scope.row.approveStatus == 1 ? '已通过' : (scope.row.approveStatus == '2' ? '已拒绝' : '未知'))}}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination background align='right' @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum" :page-size="queryParams.pageSize"
          layout="prev, pager, next" :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="checkOut" :model="checkOut" label-width="120px" :rules="rules">
        <el-form-item label="请选审批状态" prop="approveStatus">
          <el-select v-model="checkOut.approveStatus" placeholder="请选审批状态">
            <el-option v-for="item in checkOutTypeList" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="btn('checkOut')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getRecordList,
    approveCheckOut
  } from "@/api/order/order"
  export default {
    data() {
      return {
        loading: true,
        total: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        fromData: {
          createBeginTime: "",
          createEndTime: "",
          pageNum: 1,
          pageSize: 10
        },
        tableData: [],
        dialogVisible: false,
        checkOut: {
          approveStatus: '',
          id: ''
        },
        checkOutTypeList: [{
            value: 1,
            label: '通过'
          },
          {
            value: 2,
            label: '拒绝'
          }
        ],
        rules: {
          approveStatus: [{
            required: true,
            message: '请选择审批状态',
            trigger: 'blur'
          }]
        },
        id: ''
      }
    },
    created() {
      this.getRecordList()
    },
    methods: {
      getRecordList() {
       this.fromData.pageNum = this.queryParams.pageNum
       this.fromData.pageSize = this.queryParams.pageSize
        getRecordList(this.fromData).then(res => {
          this.tableData = res.data.rows
          this.total = res.data.total
          this.loading = false
        })
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleSizeChange(val) {
        this.queryParams.pageNum = val
        this.getRecordList()
      },
      handleCurrentChange(val) {
        this.queryParams.pageNum = val
        this.getRecordList()
      },
      fromCheckOut(id) {
        this.checkOut = {
          approveStatus: '',
          id: ''
        }
        this.id = id
        this.dialogVisible = true
      },
      btn(checkOut) {
        this.$refs[checkOut].validate((valid) => {
          if (valid) {
            this.checkOut.id = this.id
            approveCheckOut(this.checkOut).then(res => {
              this.$message({
                type: 'success',
                message: '提交成功!'
              })
              this.dialogVisible = false
              this.getRecordList()
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .roomStatus {
    color: #fff;
    border-radius: 2px;
    font-size: 12px;
    padding: 4px 0;
    cursor: pointer;
  }
</style>
